//获取选择框里面的数据
function projectTempBtnClick(obj) {
    if (obj.id == 'zhidingbaogao'){
        var temp = document.getElementById('weituodan')
        var index = temp.selectedIndex
        var value = temp.options[index].value
        console.log(value)
    }
}
//获取url中传递的参数
function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return  unescape(r[2]);
    return null;
}
//
console.log("------")
console.log(user)

// console.log(GetQueryString("attr0"))
var message
rawData = {"pj_id":GetQueryString("attr0")}
$.ajax({
    url:'projectTemp',
    type: 'post',
    data: rawData,

    success: function (data, status) {
        if (status == 'success') {
            // console.log("haixin");
            // console.log(data.pj_addr)
            var start = data.pj_start
            var fin = data.pj_fin
            $('#pj_name').text(data.pj_name)
            $('#pj_manager').text(data.pj_manager)
            $('#pj_state').text(data.pj_state)
            $('#pj_addr').text(data.pj_addr)
            $('#pj_company').text(data.pj_company)
            $('#pj_start').text(start.slice(0,10))
            $('#pj_client').text(data.pj_client)
            $('#pj_fin').text(fin.slice(0,10))
            $('#pj_addr').text(data.pj_addr)
            $('#pj_description').text(data.pj_description)
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("获得项目的基本信息出了问题");
        }
    }
})

var pj_id  = rawData.pj_id
if(user.user_type != 1 || user.user_type != 2){
    setTimeout(function () {
        console.log("查询所有的委托单")
        // console.log(data)
        $.ajax({
            url: '/depute/find_task_name',
            type: 'post',
            data: rawData,
            async:false,

            success: function (data, status) {
                if (status == 'success') {
                    console.log("查询所有的委托单")
                    console.log(data)
                    var len = data.length;
                    $('#depute-select').attr("length", '0');
                    for (i = 0; i < len; i++) {

                        $("#depute-select").append("<option value='" + data[i].task_description + "'>" + data[i].task_description + "</option>");

                    }
                }
            },

            error: function (data, status) {
                if (status == 'error') {
                    alert("获得委托单描述出了问题");
                }
            }
        })
    },200)
}
// 生成委托单的列表
setTimeout(function () {
    console.log("生成委托单的列表")
    $.ajax({
        url:'/depute/find_task',
        type:'post',
        data:{'pj_id':pj_id},
        async:false,

        success:function (data,status) {

            if(status =='success'){
                // console.log(data)
                var dataList = []
                dataList = data
                //生成列表
                // console.log("====task=====")
                // console.log(dataList)
                var dummy = []
                for (var i = 0; i<dataList.length;i++){
                    var list  = []
                    list.push(dataList[i].task_id,dataList[i].task_product,dataList[i].taskPaper_type
                        ,dataList[i].task_company,dataList[i].task_workNum,dataList[i].task_id )
                    dummy.push(list)
                }
                dummy = dummy.reverse()
                console.log(dummy)
                var pageID = 1;
                //列数 = 2
                var methodCount = 5;//列表里面的列数
                //每页显示10条数据
                var countPerPage = dummy.length;//生成的行数
                var pageCount = parseInt(dummy.length / countPerPage) + 1;
                var table = document.getElementById("select-task");
                var tbody = table.children[2];

                var inner = "<td>&nbsp;</td>";
                for (var j = 0; j < methodCount; j++) {
                    inner += "<td />";
                }
                for (var j = 0; j < countPerPage; j++) {
                    var tr = document.createElement("tr");
                    tr.innerHTML = inner;
                    tbody.appendChild(tr);
                }

                OnClick(1, 0);

                

                //生成结束
            }
        },

        error: function (data,status) {
            if(status == 'error')
                alert("获取委托单列表出了点问题")

        }
    })
},100)

var dataList = []
function find_depute() {

        var data = {
            'task_description': $('#depute-select option:selected').text(),
            'pj_id': pj_id
        }
        // console.log(data)
        $.ajax({
            url: '/depute/find_depute',
            type: 'post',
            data: data,
            async:false,
            success: function (data, status) {
                if (status == 'success') {

                    // console.log("这是我获得数据")
                    // console.log(data)
                    $.ajax({

                        url:'/depute/find_depute_message',
                        type: 'post',
                        data:data,//此时data为上次请求回来的数据
                        async:false,


                        success:function (data,status) {
                            if(status == 'success'){
                                dataList = data
                                // console.log(data)
                                //生成列表
                                console.log("=========")
                                console.log(dataList)
                                var dummy = []
                                for (var i = 0; i<dataList.length;i++){
                                    var list  = []
                                    list.push(dataList[i].wc_pipeName,dataList[i].wc_pipeNum,dataList[i].wc_spec
                                        ,dataList[i].wc_num)//,dataList[i].wc_techid, dataList[i].wc_id,dataList[i].wc_tid
                                    if (dataList[i].wc_techid != null){
                                        list.push("已分配",dataList[i].wc_id,dataList[i].wc_tid)
                                        $.ajax({
                                            url:'/depute/',
                                            type: 'post',
                                            data:data,//此时data为上次请求回来的数据
                                            async:false
                                        })
                                    }
                                    else{
                                        list.push("未分配", "未通过","分配","选择")//dataList[i].wc_id,dataList[i].wc_tid
                                    }
                                    // console.log(list)
                                    dummy.push(list)
                                    // dummy = dummy.reverse()
                                }
                                $.ajax({
                                    url:'/depute/find_tech_state',
                                    type: 'post',
                                    data:data,//此时data为上次请求回来的数据
                                    async:false
                                })

                                var pageID = 1;
                                //列数 = 2
                                var methodCount = 8;
                                //每页显示10条数据
                                var countPerPage = user.countPerPage;
                                //下面的分页框最多显示8
                                var pageButtonCount = 8;
                                var pageCount = parseInt(dummy.length / countPerPage) + 1;
                                var table = document.getElementById("distribution-task");

                                var tbody = table.children[2];
                                //每次点击删除列表
                                $('#tbody').empty()

                                var inner = "<td>&nbsp;</td>";
                                for (var j = 0; j < methodCount; j++) {
                                    inner += "<td />";
                                }
                                for (var j = 0; j < countPerPage; j++) {
                                    var tr = document.createElement("tr");
                                    tr.innerHTML = inner;
                                    tbody.appendChild(tr);
                                }

                                OnClick(1, 0);

                                // 生成翻页按钮
                                

                                //生成结束
                            }
                        },
                        error:function (data, status) {
                            if (status == 'error') {
                                $('#tbody').empty()
                                alert("没有找到数据");
                            }
                        }
                    })

                }
            },

            error: function (data, status) {
                if (status == 'error') {
                    alert("这个不行啊");
                }
            }
        })

    }

function OnClick(index, cardIndex) {
    if (index === -1) {
        if (pageID > 1) OnClick(pageID - 1, cardIndex);
        return;
    }
    if (index === -2) {
        if (pageID <= pageCount) OnClick(pageID + 1, cardIndex);
        return;
    }
    pageID = index;
    //显示数据
    for (var i = 0; i < user.countPerPage; i++) {
        if (dummy[(index - 1) * user.countPerPage + i] !== undefined) {
            for (var j = 0; j < methodCount; j++) {
                tbody.children[i].children[j].innerHTML = dummy[(index - 1) * user.countPerPage + i][j];
            }
            // 测试
            // var button = document.createElement("button");
            // var info = document.createElement("i");
            // info.setAttribute("class", "ace-icon fa fa-info-circle bigger-120");
            // button.appendChild(info);
            // button.setAttribute("class", "btn btn-xs btn-success");
            // 结束
            var attr = "";
            for (j = 6; j < 8; j++) {
                attr += j === 6 ? "?" : "&";
                attr += "attr" + j + "=" + dummy[(index - 1) * 8 + i][j];
                console.log(attr)
            }
            attr += '&pj_id=' + pj_id
            //----------------
            // button.setAttribute("onclick", "window.location.href='/newTecCard.html" + attr + "'");
            // // a.setAttribute('onclick', 'saveClickInfo(' + i + ')')
            // tbody.children[i].children[methodCount].innerHTML = button.outerHTML;
            //--------------------
        } else {
            tbody.children[i].children[0].innerHTML = "&nbsp;";
            for (j = 1; j < methodCount + 1; j++) {
                tbody.children[i].children[j].innerHTML = "";
            }
        }
    }

}
// 生成翻页按钮
function OnClick(index, cardIndex) {
    if (index === -1) {
        if (pageID > 1) OnClick(pageID - 1, cardIndex);
        return;
    }
    if (index === -2) {
        if (pageID <= pageCount) OnClick(pageID + 1, cardIndex);
        return;
    }
    pageID = index;
    //显示数据
    for (var i = 0; i < user.countPerPage; i++) {
        if (dummy[(index - 1) * user.countPerPage + i] !== undefined) {
            for (var j = 0; j < methodCount; j++) {
                tbody.children[i].children[j].innerHTML = dummy[(index - 1) * user.countPerPage + i][j];
            }
            var button = document.createElement("button");
            var info = document.createElement("i");
            info.setAttribute("class", "ace-icon fa fa-info-circle bigger-120");
            button.appendChild(info);
            button.setAttribute("class", "btn btn-xs btn-success");
            var attr = "";
            for (j = 4; j < 7; j++) {
                attr += j === 4 ? "?" : "&";
                attr += "attr" + j + "=" + dummy[(index - 1) * 8 + i][j];
                // console.log(attr)
            }

            button.setAttribute("onclick", "window.location.href='checkDepute.html" + attr + "'");
            // a.setAttribute('onclick', 'saveClickInfo(' + i + ')')
            tbody.children[i].children[methodCount].innerHTML = button.outerHTML;
        } else {
            tbody.children[i].children[0].innerHTML = "&nbsp;";
            for (j = 1; j < methodCount + 1; j++) {
                tbody.children[i].children[j].innerHTML = "";
            }
        }
    }

}